package com.wildwolf.project.wildwolf.location;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

/**
 * 位置管理器
 * @author LEON
 *
 */
public class WildwolfLocationManager {
	
	private Context context = null;
	
	private static  LocationManager  locationManager = null;
	
	private   Location location;
	
	private static WildwolfLocationManager wildwolfLocationManager = null;
	private static TelephonyManager  telephonyManager  = null;
	public  WildwolfLocationManager(Context context) {
		this.context = context;
	}
	public  static WildwolfLocationManager getInstence(Context context){
		if(wildwolfLocationManager==null){
			wildwolfLocationManager = new WildwolfLocationManager(context);
		}
		if(locationManager==null){
			locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		}
	return wildwolfLocationManager;
}
	public Location getLocation(){
		Criteria criteria = new Criteria();
		//精确度
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(true);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		String  provider =locationManager.getBestProvider(criteria, true);
		if(provider==null){
			enableLocationSettings();
			return null;
		}
		 location = locationManager.getLastKnownLocation(provider);
		if(location==null){
			location = new  Location(provider);
		}
		return  location;
	}
	public static TelephonyManager getTelephonyManager(Context context) {
		telephonyManager =	 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager;
	}
	/**
	 * 网络设置界面
	 */
	private void enableLocationSettings() {
	    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	    context.startActivity(settingsIntent);
	}
}
